package ochart.serialization.json
{
	import ochart.chartClasses.IChartObject;
	import ochart.serialization.SerializeFactory;
	import ochart.serialization.Serialzer;
	import ochart.serialization.json.JSON;
	import ochart.utils.ArrayUtil;
	import ochart.utils.ColorUtil;
	
	public class JSONSerializer extends Serialzer implements IJSONSerializer
	{
		public function JSONSerializer()
		{
		}
		
		override public function fromData(data:Object):Boolean
		{
			if(!super.fromData(data))
			{
				return false;
			}
			return fromJSON(data);
		}
		
		override public function toData():Object
		{
			return null;
		}
		
		public function fromJSON(json:Object):Boolean
		{
			if(!json || !target)
			{
				return false;
			}
			
			for each(var property:String in target.propertiesArray)
			{
				var value:*;
				if(json[property])
				{
					if(ArrayUtil.containsValue(colorProperties, property))
					{
						value = ColorUtil.fixColor(json[property]);
					}
					else
					{
						value = json[property];
					}
				}
				else if(ArrayUtil.containsValue(ignoreDefaultProperties, property))
				{
					// 忽略
					continue;
				}
				else
				{
					value = target.getDefaultValue(property);
				}
				target.serializationValue[property] = value;
				target[property] = value;
			}
			
			return true;
		}
		
		public function toJSON():JSON
		{
			if(!target)
			{
				return null;
			}
			var json:JSON = new JSON();
			return json;
		}
		
		public static const colorProperties:Array = [
			"color", "backgroundColor"
		];
		public static const ignoreDefaultProperties:Array = [
			"data", "chart", 
			"padding", "paddingLeft", "paddingRight", "paddingTop", "paddingBottom"];
	}
}